/**
 * @description 用户相关路由
 * @author wen
 * @date 2025.2.10
 * 
 */

const express = require('express');
const router = express.Router();
const { body } = require('express-validator');
const service = require('../services/userService');
const { createUpload } = require('../utils/upload');
const avatarUploader = createUpload({
  storagePath: 'public/avatars',
  allowedTypes: ['image/jpeg', 'image/png']
});
// 登录/注册校验
const vaildator = [
  body('username').isString().withMessage('用户名类型错误'),
  body('password').isString().withMessage('密码类型错误')
]

// 重置密码校验
const resetPwdVaildator = [
  body('username').isString().withMessage('用户名类型错误'),
  body('oldPassword').isString().withMessage('密码类型错误'),
  body('newPassword').isString().withMessage('密码类型错误')
]
const userInfoVaildator=[
  body('username').optional().isLength({ min: 2, max: 20 }),
  body('email').optional().isEmail(),
  body('bio').optional().isLength({ max: 500 })
]
// 用户信息修改校验

// 用户登录路由
router.post('/login', vaildator, service.login);

// 用户注册路由
router.post('/register', vaildator, service.register);

// 密码重置路由
router.post('/resetPwd', resetPwdVaildator, service.resetPwdByEmail);
// 获取所有用户接口
router.post('/allUser',service.getAllUsers)
// 头像上传接口
router.post('/avatar', avatarUploader.single('avatar'), service.handleAvatarUpload);
// 用户信息更新接口
router.put('/profile',userInfoVaildator,
  service.updateUserProfile
);
// 用户权限
router.post('/freezeUser',service.freezeUser)
// 删除用户
router.post("/deleteUser",service.deleteUser)
module.exports = router;

